<?php
global $_GPC, $_W;
$weid = $this->_weid;
$action = 'cost';

$storeid = intval($_GPC['storeid']);
$this->checkStore($storeid);
$title = $this->actions_titles[$action];
$returnid = $this->checkPermission($storeid);
$cur_store = $this->getStoreById($storeid);
$GLOBALS['frames'] = $this->getNaveMenu($storeid,$action);
load()->func('tpl');

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'main';
if ($operation == 'display') {

    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']);
        $condition .= " AND dateline >= {$starttime} AND dateline <= {$endtime} ";
    }

    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-1 month');
        $endtime = TIMESTAMP;
    }

    //所有金额
    $total_order_price = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid
 AND storeid=:storeid AND status<>-1 AND ispay=1 AND ismerge=0 and dateline
 >= :starttime and dateline <= :endtime", array(':weid' => $this->_weid, ':storeid' => $storeid, ':starttime' => $starttime,
        'endtime' => $endtime));
    $total_order_price = sprintf('%.2f', $total_order_price);
    $water_price = pdo_fetchcolumn("SELECT sum(water) FROM " . tablename('weisrc_dish_cost') . " WHERE weid=:weid
 AND storeid=:storeid and dateline
 >= :starttime and dateline <= :endtime", array(':weid' => $this->_weid, ':storeid' => $storeid, ':starttime' => $starttime,
        'endtime' => $endtime));
    $water_price = sprintf('%.2f', $water_price);
    $power_price = pdo_fetchcolumn("SELECT sum(power) FROM " . tablename('weisrc_dish_cost') . " WHERE weid=:weid
 AND storeid=:storeid and dateline
 >= :starttime and dateline <= :endtime", array(':weid' => $this->_weid, ':storeid' => $storeid, ':starttime' => $starttime,
        'endtime' => $endtime));
    $power_price = sprintf('%.2f', $power_price);
    $rent_price = pdo_fetchcolumn("SELECT sum(rent) FROM " . tablename('weisrc_dish_cost') . " WHERE weid=:weid
 AND storeid=:storeid and dateline
 >= :starttime and dateline <= :endtime", array(':weid' => $this->_weid, ':storeid' => $storeid, ':starttime' => $starttime,
        'endtime' => $endtime));
    $rent_price = sprintf('%.2f', $rent_price);
    $orther_price = pdo_fetchcolumn("SELECT sum(orther) FROM " . tablename('weisrc_dish_cost') . " WHERE weid=:weid
 AND storeid=:storeid and dateline
 >= :starttime and dateline <= :endtime", array(':weid' => $this->_weid, ':storeid' => $storeid, ':starttime' => $starttime,
        'endtime' => $endtime));
    $orther_price = sprintf('%.2f', $orther_price);
    //利润
    $profit = $total_order_price - $water_price - $power_price - $rent_price - $orther_price;

    $strwhere = " WHERE weid ={$weid} AND role<>1 AND storeid={$storeid} AND type=2 ";
    $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_account) . " $strwhere ORDER BY id DESC
");

    if ($_GPC['out_put'] == 'output') {
        $out_count = 0;
        $out_totalprice = 0;
        $out_index = 1;
        foreach ($list as $key => $row) {
            $outarr[$i]['username'] = $row['username'];
            $outarr[$i]['fee_rate'] = sprintf('%.2f', $row['fee_rate']);
            $fee =  $profit * $row['fee_rate'] / 100;
            $fee = sprintf('%.2f', $fee);
            $outarr[$i]['fee'] = $fee;
            $i++;
        }
        $outtitle[0] = array(
            'total_order_price' => $total_order_price,
            'water_price' => $water_price,
            'power_price' => $power_price,
            'rent_price' => $rent_price,
            'orther_price' => $orther_price,
            'profit' => $profit
        );

        $this->exportexcel2(
            $outtitle,
            array('营业额', '水费', '电费', '租金', '其他费用', '利润'),
            $outarr,
            array('姓名', '分红比例', '分红'),
            TIMESTAMP);
        exit();
    }


} elseif ($operation == 'list') {
    $strwhere = " AND storeid={$storeid} ";
    $pindex = max(1, intval($_GPC['page']));
    $psize = 10;
    $list = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_cost') . "  WHERE weid = :weid $strwhere ORDER BY id DESC LIMIT
" . ($pindex - 1) * $psize . ',' . $psize, array(':weid' => $this->_weid));

    if (!empty($list)) {
        $total = pdo_fetchcolumn('SELECT COUNT(1) FROM ' . tablename('weisrc_dish_cost') . " WHERE weid = :weid $strwhere", array(':weid' => $this->_weid));
        $pager = pagination($total, $pindex, $psize);
    }
} else if ($operation == 'post') {
    $id = intval($_GPC['id']);
    $item = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_cost') . " WHERE weid = :weid AND id=:id AND storeid=:storeid ORDER BY id DESC", array(':weid' => $this->_weid, ':id' => $id, ':storeid' => $storeid));

    if (checksubmit('submit')) {

        $data = array(
            'weid' => $this->_weid,
            'storeid' => intval($_GPC['storeid']),
            'water' => floatval($_GPC['water']),
            'power' => floatval($_GPC['power']),
            'rent' => floatval($_GPC['rent']),
            'orther' => floatval($_GPC['orther']),
            'dateline' => strtotime($_GPC['dateline']),
        );

        if (empty($id)) {
            pdo_insert('weisrc_dish_cost', $data);
        } else {
            pdo_update('weisrc_dish_cost', $data, array('id' => $id));
        }
        message('操作成功！', $this->createWebUrl('cost', array('op' => 'display', 'storeid' => $storeid), true));
    }
} else if ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $item = pdo_fetch("SELECT id FROM " . tablename('weisrc_dish_cost') . " WHERE id = :id AND weid=:weid ", array(':id' => $id, ':weid' => $_W['uniacid']));
    if (empty($item)) {
        message('抱歉，不存在或是已经被删除！', $this->createWebUrl('cost', array('op' => 'display')), 'error');
    }
    pdo_delete('weisrc_dish_cost', array('id' => $id, 'weid' => $_W['uniacid']));
    message('删除成功！', $this->createWebUrl('cost', array('op' => 'list', 'storeid' => $storeid)), 'success');
}
include $this->template('web/cost');